
#ifndef _NETWORK_H
#define	_NETWORK_H

#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/time.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <cctype>
#include <ifaddrs.h>

#include <iostream>
#include <cstring>

using namespace std;
/**
 * The network class. Functionality comments are in the source file.
 */
class network
{
public:
    network();
    virtual ~network();
    int Start(char *ip, unsigned short port);

    network& operator >> (int len);
    network& operator << (char *data);
    network& operator << (unsigned char data);
    network& operator << (unsigned short data);
    network& operator << (unsigned int data);

    bool Send(int socket, unsigned char packetid);
    
protected:
    struct LList_socks_t
    {
        int sock;
        int permissions;
        char username[64];
        LList_socks_t *prev;
        LList_socks_t *next;
    };

private:
    // Structres and Functions for receiving data

    struct socket_holder
    {
        char addr[INET_ADDRSTRLEN];
        unsigned short port;

        int listener;
        LList_socks_t *socks;
        fd_set ms;
        int maxfd;
    } net;

    int Recv();
    int Listen();
    LList_socks_t *Close(LList_socks_t *list);
    int Accept();
    bool GetLocalIP();

    virtual bool Parse(int socket, LList_socks_t *socks, char *data, int datalen) { return false; }

    // Structures and Functions for Sending data
    char sendbuf[65536];
    int pos;
    int nextlen;

};


#endif
